package com.by.toolkit.utils;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * 描述：多个条件判断
 * <p>
 * if(objects.isnull(a)||objects.isnull(b)) ---> if(anyNull(a,b))
 *
 * @author liujun
 * @date 2023年06月30 8:54 PM
 */
public class ByGroupUtil {
    private ByGroupUtil() {
    }

    /**
     * 任意为空,
     * 注意只能处理平级的
     * 如果是 anyNull(object, object.getName()) 会抛空指针
     */
    public static boolean anyNull(Object... objects) {
        if (objects == null || objects.length == 0) {
            return Boolean.TRUE;
        }
        return Stream.of(objects).anyMatch(Objects::isNull);
    }


    /**
     * 任意非空
     *
     * @param objects canshu
     * @return 结果
     */
    public static boolean anyNotNull(Object... objects) {
        if (objects == null || objects.length == 0) {
            return Boolean.FALSE;
        }
        return Stream.of(objects).anyMatch(Objects::nonNull);
    }

    /**
     * 全为空
     *
     * @param objects o
     * @return r
     */
    public static boolean allNull(Object... objects) {
        return !anyNotNull(objects);
    }

    /**
     * 全不空
     *
     * @param objects o
     * @return r
     */
    public static boolean allNotNull(Object... objects) {
        return !anyNull(objects);
    }

    /**
     * 任意空
     */
    public static boolean anyEmpty(String... strings) {
        if (strings == null || strings.length == 0) {
            return Boolean.TRUE;
        }
        return Stream.of(strings).anyMatch(StringUtils::isEmpty);
    }

    /**
     * 任意非空
     */
    public static boolean anyNotEmpty(String... strings) {
        if (strings == null || strings.length == 0) {
            return Boolean.FALSE;
        }
        return Stream.of(strings).anyMatch(StringUtils::isNotEmpty);
    }

    /**
     * 全空=没有任意非空
     */
    public static boolean allEmpty(String... strings) {
        return !anyNotEmpty(strings);
    }

    /**
     * 全非空=没有任意空
     */
    public static boolean allNotEmpty(String... strings) {
        return !anyEmpty(strings);
    }


    /**
     * 任意空
     */
    public static boolean anyEmpty(Collection... collections) {
        if (collections == null || collections.length == 0) {
            return Boolean.TRUE;
        }
        return Stream.of(collections).anyMatch(CollectionUtils::isEmpty);
    }

    public static boolean anyNotEmpty(Collection... collections) {
        if (collections == null || collections.length == 0) {
            return Boolean.FALSE;
        }
        return Stream.of(collections).anyMatch(CollectionUtils::isNotEmpty);
    }

    public static boolean allEmpty(Collection... collection) {
        return !anyNotEmpty(collection);
    }

    public static boolean allNotEmpty(Collection... collection) {
        return !anyEmpty(collection);
    }


    public static boolean anyTrue(Boolean... res) {
        checkNull(res);
        return Arrays.asList(res).contains(Boolean.TRUE);
    }

    public static boolean anyFalse(Boolean... res) {
        checkNull(res);
        return Arrays.asList(res).contains(Boolean.FALSE);

    }

    public static boolean allFalse(Boolean... res) {
        checkNull(res);
        return !anyTrue(res);
    }

    public static boolean allTrue(Boolean... res) {
        checkNull(res);
        return !anyFalse(res);
    }

    private static void checkNull(Boolean[] res) {
        if (res == null || res.length == 0) {
            throw new InvalidParameterException("对错判断参数不能为空");
        }
        if (Stream.of(res).anyMatch(Objects::isNull)) {
            throw new InvalidParameterException("对错判断参数不能出现空");
        }
    }

    public static void main(String[] args) {
        System.out.println(anyTrue(Boolean.FALSE, Boolean.TRUE));
        System.out.println(anyNull());
//        System.out.println(anyFalse());
        System.out.println(anyNotNull(null, new Object()));

    }
}
